package com.ruoyi.system.service.impl;

import java.util.*;
import java.util.stream.Collectors;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.domain.TreeSelect;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.dto.TChannelSortDto;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.ChannelSortMapper;
import com.ruoyi.common.core.domain.entity.ChannelSort;
import com.ruoyi.system.service.IChannelSortService;

/**
 * 渠道分类Service业务层处理
 * 
 * @author yanghuiliao
 * @date 2022-11-29
 */
@Service
public class ChannelSortServiceImpl implements IChannelSortService 
{
    @Autowired
    private ChannelSortMapper channelSortMapper;

    /**
     * 查询渠道分类
     * 
     * @param id 渠道分类ID
     * @return 渠道分类
     */
    @Override
    public ChannelSort selectChannelSortById(Long id)
    {
        return channelSortMapper.selectChannelSortById(id);
    }

    /**
     * 查询渠道分类列表
     * 
     * @param channelSort 渠道分类
     * @return 渠道分类
     */
    @Override
    public List<ChannelSort> selectChannelSortList(ChannelSort channelSort)
    {
        return channelSortMapper.selectChannelSortList(channelSort);
    }

    /**
     * 新增渠道分类
     * 
     * @param channelSort 渠道分类
     * @return 结果
     */
    @Override
    public int insertChannelSort(ChannelSort channelSort)
    {
        ChannelSort sort = channelSortMapper.selectChannelSortById(channelSort.getParentId());
        // 如果父节点不为正常状态,则不允许新增子节点
        if (!UserConstants.DEPT_NORMAL.equals(sort.getStatus()))
        {
            throw new CustomException("部门停用，不允许新增");
        }
        channelSort.setAncestors(sort.getAncestors() + "," + channelSort.getParentId());
        return channelSortMapper.insertChannelSort(channelSort);
    }

    /**
     * 修改渠道分类
     * 
     * @param channelSort 渠道分类
     * @return 结果
     */
    @Override
    public int updateChannelSort(ChannelSort channelSort)
    {
        channelSort.setUpdateTime(DateUtils.getNowDate());
        return channelSortMapper.updateChannelSort(channelSort);
    }

    /**
     * 删除渠道分类信息
     * 
     * @param id 渠道分类ID
     * @return 结果
     */
    @Override
    public int deleteChannelSortById(Long id)
    {
        return channelSortMapper.deleteChannelSortById(id);
    }

    @Override
    public List<TreeSelect> buildDeptTreeSelect(List<ChannelSort> channelSorts) {
        List<ChannelSort> deptTrees = buildDeptTree(channelSorts);
        return deptTrees.stream().map(TreeSelect::new).collect(Collectors.toList());
    }

    /**
     * 构建前端所需要树结构
     *
     * @param channelSorts 部门列表
     * @return 树结构列表
     */
    @Override
    public List<ChannelSort> buildDeptTree(List<ChannelSort> channelSorts)
    {
        List<ChannelSort> returnList = new ArrayList<ChannelSort>();
        List<Long> tempList = new ArrayList<Long>();
        for (ChannelSort channelSort : channelSorts)
        {
            tempList.add(channelSort.getId());
        }
        for (Iterator<ChannelSort> iterator = channelSorts.iterator(); iterator.hasNext();)
        {
            ChannelSort channelSort = (ChannelSort) iterator.next();
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(channelSort.getParentId()))
            {
                recursionFn(channelSorts, channelSort);
                returnList.add(channelSort);
            }
        }
        if (returnList.isEmpty())
        {
            returnList = channelSorts;
        }
        return returnList;
    }

    /**
     * 递归列表
     */
    private void recursionFn(List<ChannelSort> list, ChannelSort t)
    {
        // 得到子节点列表
        List<ChannelSort> childList = getChildList(list, t);
        t.setChildren(childList);
        for (ChannelSort tChild : childList)
        {
            if (hasChild(list, tChild))
            {
                recursionFn(list, tChild);
            }
        }
    }

    /**
     * 得到子节点列表
     */
    private List<ChannelSort> getChildList(List<ChannelSort> list, ChannelSort t)
    {
        List<ChannelSort> tlist = new ArrayList<ChannelSort>();
        Iterator<ChannelSort> it = list.iterator();
        while (it.hasNext())
        {
            ChannelSort n = (ChannelSort) it.next();
            if (StringUtils.isNotNull(n.getParentId()) && n.getParentId().longValue() == t.getId().longValue())
            {
                tlist.add(n);
            }
        }
        return tlist;
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<ChannelSort> list, ChannelSort t)
    {
        return getChildList(list, t).size() > 0 ? true : false;
    }

    @Override
    public String checkChannelSortUnique(ChannelSort dept) {
        Long id = StringUtils.isNull(dept.getId()) ? -1L : dept.getId();
        ChannelSort info = channelSortMapper.checkChannelSortUnique(dept.getName(), dept.getParentId());
        if (StringUtils.isNotNull(info) && info.getId().longValue() != id.longValue())
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    /**
     * 是否存在子节点
     *
     * @param id 部门ID
     * @return 结果
     */
    @Override
    public boolean hasChildByDeptId(Long id)
    {
        int result = channelSortMapper.hasChildByDeptId(id);
        return result > 0 ? true : false;
    }

    @Override
    public boolean checkDeptExistUser(Long id) {
        int result = channelSortMapper.checkDeptExistUser(id);
        return result > 0 ? true : false;
    }
}
